package cn.com.czpb.client.utils;

import android.app.Activity;
import android.content.ContentResolver;
import android.provider.Settings;
import android.support.v7.app.AppCompatActivity;
import android.view.WindowManager;

/**
 * Created by 猪猪侠 on 2017/6/30.
 */

public class ReadModeUtils {
    public static void setNightOrDayMode(AppCompatActivity baseActivity) {
        int readMode = 1;
//        int readMode = DefaultUserUtils.getReadMode();
        int brightValue = 0;
        switch (readMode) {
            case 1: //day模式
                boolean isAutoBrightness = isAutoBrightness(baseActivity);
                if (isAutoBrightness) {
                    brightValue = -1;
                } else {
                    brightValue = getBrightness(baseActivity);
                }
                break;
            case 0: //night模式
                brightValue = 20;
                break;
        }
        setBrightness(baseActivity, brightValue);
    }

    public static boolean isAutoBrightness(Activity activity) {
        boolean autoBrightness = false;
        ContentResolver contentResolver = activity.getContentResolver();
        try {
            autoBrightness = Settings.System.getInt(contentResolver,
                    Settings.System.SCREEN_BRIGHTNESS_MODE) == Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC;
        } catch (Settings.SettingNotFoundException e) {
            e.printStackTrace();
        }
        return autoBrightness;
    }

    private static int getBrightness(Activity activity) {
        int brightValue = 0;
        ContentResolver contentResolver = activity.getContentResolver();
        try {
            brightValue = Settings.System.getInt(contentResolver, Settings.System.SCREEN_BRIGHTNESS);
        } catch (Settings.SettingNotFoundException e) {
            e.printStackTrace();
        }
        return brightValue;
    }

    private static void setBrightness(Activity activity, int brightValue) {
        WindowManager.LayoutParams lp = activity.getWindow().getAttributes();
        lp.screenBrightness = (brightValue <= 0 ? -1.0f : brightValue / 255f);
        activity.getWindow().setAttributes(lp);
    }

//    public static void setScreenBrightness(int value) {
//        setScrennManualMode();
//        ContentResolver mContentResolver = BaseApplication.getInstance().getContentResolver();
//        Settings.System.putInt(mContentResolver, Settings.System.SCREEN_BRIGHTNESS, value);
//    }
//
//    public static int getScreenBrightness() {
//        ContentResolver contentResolver = BaseApplication.getInstance().getContentResolver();
//        int defVal = 125;
//        return Settings.System.getInt(contentResolver, Settings.System.SCREEN_BRIGHTNESS, defVal);
//    }
//
//    private static void setScrennManualMode() {
//        ContentResolver contentResolver = BaseApplication.getInstance().getContentResolver();
//        try {
//            int mode = Settings.System.getInt(contentResolver, Settings.System.SCREEN_BRIGHTNESS_MODE);
//            if (mode == Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC) {
//                Settings.System.putInt(contentResolver, Settings.System.SCREEN_BRIGHTNESS_MODE, Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);
//            }
//        } catch (Settings.SettingNotFoundException e) {
//            e.printStackTrace();
//        }
//    }
}
